/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Dao;

import Controler.CXuLy;
import Model.Pojo.Danhmucsanpham;
import Model.Pojo.Parameters;
import Model.Util.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author THANH QUAN
 */
public class DAODanhMucSanPham
{

    public List<Danhmucsanpham> DanhSachDanhMuc() throws Exception
    {
        List<Danhmucsanpham> dsSanPham = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from Danhmucsanpham dmsp where dmsp.tinhtrang = True";

            dsSanPham = DAOProvider.Select(session, hql, new Parameters[]
            {
            });
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
        return dsSanPham;
    }

    public boolean KiemTraDanhMucTonTai(Danhmucsanpham dmsp) throws Exception
    {
        List<Danhmucsanpham> dsDanhMuc = DanhSachDanhMuc();
        for (Danhmucsanpham dm : dsDanhMuc)
        {
            if (dm.getTendanhmuc().equals(dmsp.getTendanhmuc()) && dm.getKyhieu().equals(dmsp.
                    getKyhieu()))
            {
                return true;
            }
        }
        return false;
    }

    public void ThemDanhMuc(Danhmucsanpham dmsp) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            DAOProvider.Insert(session, dmsp);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    public Danhmucsanpham GetDanhmucsanpham(int id) throws Exception
    {
        Danhmucsanpham dmsp = null;
        Session session = HibernateUtil.getSessionFactory().openSession();


        try
        {
            dmsp = (Danhmucsanpham) DAOProvider.SelectOne(session, Danhmucsanpham.class, id);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
        return dmsp;
    }

    public void DeleteDanhMuc(int id) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Danhmucsanpham dmsp = GetDanhmucsanpham(id);
        if (dmsp == null)
        {
            return;
        }
        try
        {
            dmsp.setTinhtrang(Boolean.FALSE);
            DAOProvider.Update(session, dmsp);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    public void UpdateDanhMuc(Danhmucsanpham dmsp) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (dmsp == null)
        {
            return;
        }
        try
        {
            DAOProvider.Update(session, dmsp);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * *
     * Tim kiem danh muc
     *
     * @param kyhieu
     * @param tendanhmuc
     * @return
     */
    public List<Danhmucsanpham> TimKiemDanhMuc(String kyhieu, String tendanhmuc) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        String hql = "";
        if (!"".equals(kyhieu))
        {
            hql = "from Danhmucsanpham dm where dm.tinhtrang=true and dm.kyhieu =:kyhieu ";
            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("kyhieu", kyhieu)
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }
        else
        {
            hql = "from Danhmucsanpham dm where dm.tinhtrang=true and dm.tendanhmuc like :tendanhmuc ";

            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("tendanhmuc", "%" + tendanhmuc + "%")
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }
    }
}
